using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class SettingPanel : UIPanel
{
    private Slider slider_bgm;
    private Slider slider_sound;
    
    private Button btnHome;

    private void Awake()
    {
        btnHome     = transform.Find("ReturnButton").GetComponent<Button>();
        btnHome.onClick.AddListener(OnClickHome);
        slider_bgm   = transform.Find("Music").GetComponentInChildren<Slider>();
        slider_sound = transform.Find("Sound").GetComponentInChildren<Slider>();
        slider_bgm.onValueChanged.AddListener(OnSliderBgmValueChanged); 
        slider_sound.onValueChanged.AddListener(OnSliderSoundValueChanged);
    }

    public override void Init()
    {
        slider_bgm.value   = PlayerPrefs.GetFloat(Const.BGM,   0.5f);
        slider_sound.value = PlayerPrefs.GetFloat(Const.SOUND, 0.5f);
    }
    
    void OnClickHome()
    {
        Hide();
        UIManager.Instance.OpenPanel<MenuPanel>();
    }

    void OnSliderBgmValueChanged(float value)
    {
        AudioManager.instance.OnBgmVolumeChange(value);
        PlayerPrefs.SetFloat(Const.BGM,value);
    }
    
    void OnSliderSoundValueChanged(float value)
    {
        AudioManager.instance.OnSoundVolumeChange(value);
        PlayerPrefs.SetFloat(Const.SOUND,value);
    }
}
